package com.syh.jn.scm.service.impl.supplier.base.downstream;

import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.base.downstream.DownstreamCategory;
import com.syh.jn.scm.domain.supplier.repository.base.downstream.DownstreamCategoryRepository;
import com.syh.jn.scm.dto.supplier.base.downstream.DownstreamCategoryMto;
import com.syh.jn.scm.dto.supplier.base.downstream.DownstreamCategorySto;
import com.syh.jn.scm.dto.supplier.base.downstream.DownstreamCategoryVo;
import com.syh.jn.scm.service.api.supplier.base.downstream.DownstreamCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DownstreamCategoryServiceImpl extends AbstractServiceImpl<DownstreamCategorySto, DownstreamCategoryMto, DownstreamCategoryVo, DownstreamCategory, Long> implements DownstreamCategoryService {

  @Autowired
  private DownstreamCategoryRepository downstreamCategoryRepository;

  @Override
  protected AggregateRepository<DownstreamCategory, Long> getAggregateRepository() {
    return downstreamCategoryRepository;
  }

  /**
   * 新增对象
   *
   * @param sto
   * @return
   */
  @Override
  public Long save(DownstreamCategorySto sto) {
    DownstreamCategory category = new DownstreamCategory(sto);
    return downstreamCategoryRepository.add(category);
  }

  /**
   * 修改对象
   *
   * @param mto
   */
  @Override
  public void update(DownstreamCategoryMto mto) {
    DownstreamCategory category = downstreamCategoryRepository.getById(mto.getId());
    category.modify(mto);
    downstreamCategoryRepository.update(category);
  }

  /**
   * 删除对象
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    DownstreamCategory category = downstreamCategoryRepository.getById(id);
    category.destroy();
    downstreamCategoryRepository.remove(category);
  }

  /**
   * 启用
   *
   * @param id
   */
  @Override
  public void enabled(Long id) {
    this.recursionEnabled(id);
  }

  /**
   * 递归启用
   *
   * @param id
   */
  private void recursionEnabled(Long id) {
    DownstreamCategory category = downstreamCategoryRepository.getById(id);
    //先启用父
    if (!Long.valueOf("0").equals(category.getSupId())) {
      DownstreamCategory categorySup = downstreamCategoryRepository.getById(category.getSupId());
      categorySup.enabled();
      downstreamCategoryRepository.update(categorySup);
      recursionEnabled(categorySup.getId());
    }
    //再启用子
    category.enabled();
    downstreamCategoryRepository.update(category);
  }

  /**
   * 停用
   *
   * @param id
   */
  @Override
  public void disabled(Long id) {
    DownstreamCategory category = downstreamCategoryRepository.getById(id);
    //先把所有子停用
    Wrapper cateWrapper = Wrapper.build();
    cateWrapper.append(Restrictions.like("catalogCode", category.getCatalogCode()));
    List<DownstreamCategory> downstreamCategories = downstreamCategoryRepository.getList(cateWrapper);
    if (downstreamCategories != null && downstreamCategories.size() > 0) {
      downstreamCategories.stream().forEach((downstreamCategory) -> {
        downstreamCategory.disabled();
        downstreamCategoryRepository.update(downstreamCategory);
      });
    }
    //再停用父的
    category.disabled();
    downstreamCategoryRepository.update(category);
  }
}